package com.demo2do.fms.admin.service.data.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;
import com.demo2do.fms.admin.dao.data.DriverSalaryDao;
import com.demo2do.fms.admin.service.DataServiceImpl;
import com.demo2do.fms.admin.service.data.DriverSalaryService;
import com.demo2do.fms.entity.authority.Organization;
import com.demo2do.fms.entity.data.cost.DriverSalary;

/**
 * 
 * @author Chang Liu
 */
@Service("driverSalaryService")
public class DriverSalaryServiceImpl extends DataServiceImpl<DriverSalary>implements DriverSalaryService{
    
    @Dao
    private DriverSalaryDao driverSalaryDao;

    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.data.DriverSalaryService#count(java.util.List)
     */
    public int count(List<Long> orgIds) {
        
        //if orgIds is null, return 0;
        if(CollectionUtils.isEmpty(orgIds))
            return 0;
        
        QueryFilter queryFilter = new QueryFilter().addSentence("organization.id IN (:orgIds)", "orgIds", orgIds);
        return super.count(DriverSalary.class, queryFilter);
        
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.data.DriverSalaryService#list(java.util.List, com.demo2do.core.entity.page.Page)
     */
    public List<DriverSalary> list(List<Long> orgIds, Page page) {
        
        //if orgIds is null, return null;
        if(CollectionUtils.isEmpty(orgIds))
            return null;
        
        QueryFilter queryFilter = new QueryFilter().addSentence("organization.id IN (:orgIds)", "orgIds", orgIds);
        QueryOrder queryOrder = new QueryOrder().add("organization.name", "ASC");
        page.calculate();
        return super.listByPage(DriverSalary.class, queryFilter, queryOrder, page);
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.data.DriverSalaryService#getDriverSalary(java.lang.Long, java.lang.Integer)
     */
    public DriverSalary getDriverSalary(Long orgId, Integer driverCount) {
        
        return driverSalaryDao.getDriverSalary(orgId, driverCount);
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.data.DriverSalaryService#create(java.lang.Long, com.demo2do.fms.entity.authority.Organization, java.lang.Integer, com.demo2do.fms.entity.data.cost.DriverSalary)
     */
    public void create(Long operator , Organization organization, Integer driverCount, DriverSalary driverSalary) {
        
        //load DriverSalary according to orgId and driverCount
        DriverSalary oldDriverSalary = driverSalaryDao.getDriverSalary(organization.getId(), driverCount);
        
        if(oldDriverSalary !=null){
            
            // 将oldDriverSalary的endDate设成now 
            genericDaoSupport.update(oldDriverSalary.disable()); 
         
        }
        
        // 将新的salary的startDate设成now
        genericDaoSupport.save(driverSalary.init().operator(operator));
        
        
        
    }
    
}
